#pragma once

#include <QColor>
#include <QFrame>
#include <QLabel>
#include <QMouseEvent>
#include <QPoint>
#include <map>
#include <vector>

#include "widgets.h"

class STabLabel : public QLabel {
  Q_OBJECT
 public:
  STabLabel(const QString&, QWidget* parent = nullptr);
  ~STabLabel();
  void setIndex(int v) { m_index = v; }
  int index() const { return m_index; }
  void setImgName(QString imgName) { m_imgName = imgName; }
  void setImgTip(QString imgTip) { m_imgTip = imgTip; }
  void setDragable(bool v);
  void showSelBorder(bool show, QString clrFg);
  void setBkColor(QColor c) { m_bkColor = c; }
  QColor bkColor() const { return m_bkColor; }
 Q_SIGNALS:
  void clicked(int);
  void tabDragged(int, int);

 protected:
  void mousePressEvent(QMouseEvent* event) override;
  void mouseReleaseEvent(QMouseEvent* ev) override;
  void paintEvent(QPaintEvent*) override;
  void mouseMoveEvent(QMouseEvent* event) override;
  void dragEnterEvent(QDragEnterEvent* event) override;
  void dropEvent(QDropEvent* event) override;
  void dragMoveEvent(QDragMoveEvent* event) override;

 private:
  int m_index = 0;
  QString m_imgName;
  QString m_imgTip;
  QRect m_ImageRect;
  bool m_canDrag = false;
  QPoint m_dragPos;
  QColor m_bkColor;
};

class S_WIDGETS_EXPORT STabCtrl : public QFrame {
  Q_OBJECT

 public:
  STabCtrl(QWidget* p);
  void addTab(const QString& name, const QString& tip = "");
  void setTabBkColor(int index, QColor color);
  void setTabInfo(int index, const QString& imgName, const QString& tip = "");
  void selectTab(int index);
  void setTabDisabled(int index);
  void setTabText(int index, const QString& name);
  void showSelBorder(int index, bool show, QString clrFg);
  int getTabCount();
  int getSelTab();
  int getLastSelTab();
  void setTabData(int index, QVariant v);
  QVariant tabData(int index);
  void setTabDragable(int index, bool v);

 signals:
  void tabSelected(int index);
  void tabSeqChanged();

 protected:
  void showEvent(QShowEvent* event) override;
  void unHoverLabel(int index);
 protected Q_SLOTS:
  void onTabClicked(int);
  void onTabDragged(int, int);

 private:
  std::vector<STabLabel*> m_labels;
  std::map<int, QColor> m_bkColors;
  QString m_styleSheet;
  int m_selIndex = -1;
  int m_lastSelIndex = -1;
};
